home *** CD-ROM | disk | FTP | other *** search
- /*
- messengr.cpp
-
- Message retrieval class
-
- C++/Views 2.0 Demo
- Copyright (c) 1992, by Liant Software Corp.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- */
-
- #include "messengr.h"
- #include "file.h"
- #include "tagstrm.h"
- #include "dictinry.h"
-
- Messenger::Messenger(char *filename)
- /*
- Construct a Messenger object.
-
- A messager object provides a mechanism for retrieving text messages
- from a file.
-
- Each message is preceded by an %item% label (delimited by '%' characters).
-
- You can retrieve the message associated with a particulay item
- with Messenger::getMessage(char *item).
- */
- {
- lastoffs = 0L;
-
- currMsg.puts("");
-
- msgFile = new VFile(filename);
-
- if (!msgFile->open(ReadOnly)) {
- delete msgFile;
- msgFile = 0;
- tagStrm = 0;
- idxDict = 0;
- }
- else {
- tagStrm = new VTagStream();
- tagStrm->beginScan(msgFile, NIL);
- tagStrm->tags('%', '%');
-
- idxDict = new VDictionary();
- }
- }
-
- Messenger::~Messenger()
- {
- if (msgFile) {
- msgFile->close();
- delete msgFile;
- delete tagStrm;
- delete idxDict;
- }
- }
-
- VStream &Messenger::getMessage(char *item)
- /*
- Find message 'item' in the message file.
- */
- {
- VString index;
- long offset;
-
- currMsg.reset();
-
- if (!msgFile) {
- currMsg.puts("Unable to find message file.");
- return(currMsg);
- }
-
- /* set default error message */
- currMsg.puts("Message item not found");
-
- if (tagStrm) {
- /* search for tag "item" */
- tagStrm->beginScan(msgFile, NIL);
-
- /* first, see if we've already found the message */
- if ((offset = (long) idxDict->getValueAtKey(&VString(item))) != 0L) {
- tagStrm->fromAt(offset);
-
- /* found it: read in the message */
- tagStrm->skipTo('\n');
- tagStrm->scan(msgFile, &currMsg);
- tagStrm->toAt(0L);
- tagStrm->getTag(index);
- tagStrm->ungetTo();
- currMsg.putch('\0');
- }
- else {
- /* search for the message (start from the end) */
- tagStrm->fromAt(lastoffs);
-
- while(tagStrm->getTag(index)) {
- /* save the file offset of the message */
- offset = (long) tagStrm->fromAt();
- idxDict->atKeyPut(new VString(index.gets()), offset);
- lastoffs = offset;
-
- if (index == item) {
- /* found the one we're looking for, read it in */
- tagStrm->skipTo('\n');
- tagStrm->scan(msgFile, &currMsg);
- tagStrm->toAt(0L);
- tagStrm->getTag(index);
- tagStrm->ungetTo();
- currMsg.putch('\0');
- break;
- }
- }
- }
- }
-
- return(currMsg);
- }
-
-